package org.particlethink.controller;

import org.particlethink.auth.exception.TokenExpireException;
import org.particlethink.exception.TokenException;
import org.particlethink.response.BaseResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class ExceptionController {

    @ExceptionHandler(value = TokenExpireException.class)
    public BaseResponse<?> handleTokenExpireException(final TokenExpireException ex) {
        return BaseResponse.fail(ex.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public BaseResponse<?> failResponse(Exception ex) {
        ex.printStackTrace();
        return BaseResponse.fail(ex.getMessage());
    }


    @ExceptionHandler(value = TokenException.class)
    public BaseResponse<?> handleTokenExpireResponse(HttpServletResponse response, Exception ex) {
//        ex.printStackTrace();
        response.setStatus(401);
        return BaseResponse.fail(401, ex.getMessage());
    }

}
